package com.hnust.controller.personInfo;

import com.hnust.controller.MainController;
import com.hnust.domain.College;
import com.hnust.fx.DialogBuilder;
import com.hnust.fx.FXMLController;
import com.hnust.service.PersonalDataServices;
import com.hnust.store.DataStore;
import com.hnust.view.personInfo.MyCourseGroupView;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXPasswordField;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.StringConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class PersonDataController implements Initializable {
    public HBox personalDataWindow;
    public VBox leftContent;
    public VBox rightContent;
    public VBox gap;
    public JFXPasswordField teacherPassword;
    public JFXListView<StackPane> courseGroup;
    public VBox loadingCourseGroup;
    public VBox loadingInfo;
    public GridPane teacherInfoContainer;
    public JFXButton teacherInfoBtn;
    public Label collegeLabel;
    public JFXButton joinCourseGroupBtn;
    public JFXButton exitCourseGroupBtn;
    private String selectCollegeId;
    private String selectCourseGroupId="";
    private String selectCourseGroupState="";
    @FXML
    public ComboBox<College> college;
    @Autowired
    MainController mainController;
    @Autowired
    MyCourseGroupView myCourseGroupView;

    @Autowired
    private PersonalDataServices personalDataServices;

    @Autowired
    private DataStore dataStore;
    @FXML
    public TextField teacherNumber;
    @FXML
    public TextField teacherName;
    @FXML
    public TextField teacherPhone;
    @FXML
    public TextField teacherMail;


    private static final Logger logger = LogManager.getLogger(PersonDataController.class);

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //屏幕适配
        leftContent.prefWidthProperty().bind(personalDataWindow.widthProperty().multiply(0.5));
        rightContent.prefWidthProperty().bind(personalDataWindow.widthProperty().multiply(0.5));
        leftContent.prefHeightProperty().bind(personalDataWindow.heightProperty());
        rightContent.prefHeightProperty().bind(personalDataWindow.heightProperty());
        gap.prefHeightProperty().bind(personalDataWindow.heightProperty());
        gap.prefWidthProperty().bind(personalDataWindow.widthProperty().multiply(0.05));
        college.prefWidthProperty().bind(leftContent.widthProperty().multiply(0.75));
        teacherNumber.prefWidthProperty().bind(leftContent.widthProperty().multiply(0.75));
        teacherName.prefWidthProperty().bind(leftContent.widthProperty().multiply(0.75));
        teacherPhone.prefWidthProperty().bind(leftContent.widthProperty().multiply(0.75));
        teacherPassword.prefWidthProperty().bind(leftContent.widthProperty().multiply(0.75));
        teacherMail.prefWidthProperty().bind(leftContent.widthProperty().multiply(0.75));
        teacherPassword.prefWidthProperty().bind(leftContent.widthProperty().multiply(0.75));
        courseGroup.prefHeightProperty().bind(rightContent.heightProperty().multiply(0.7));
        loadingCourseGroup.prefHeightProperty().bind(rightContent.heightProperty().multiply(0.7));
        courseGroup.propagateMouseEventsToParent();

        //数据转换
        college.setConverter(new StringConverter<College>() {
            @Override
            public String toString(College object) {
                return object.getName();
            }
            @Override
            public College fromString(String string) {
                return null;
            }
        });
        college.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                selectCollegeId=newValue.getColId();
                logger.info("选中的学院ID："+selectCollegeId);
            }
        });
        courseGroup.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                selectCourseGroupId=((Label)newValue.getChildren().get(0)).getText();
                HBox h=(HBox)newValue.getChildren().get(2);
                selectCourseGroupState=((Label)h.getChildren().get(0)).getText();
                logger.info("选中的课程组ID："+selectCourseGroupId);
                logger.info("选中的课程组状态："+selectCourseGroupState);
            }
        });

        personalDataWindow.parentProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                logger.info("进入个人信息页面");
                getUserInfoData();
                getCourseGroupData();
            }else{
                selectCourseGroupId="";
                selectCourseGroupState="";
            }
        });
    }

    public void getCourseGroupData() {
        personalDataServices.getCourseGroupData(courseGroup,dataStore.getCollege_id(),dataStore.getTeacher_id(),
                loadingCourseGroup);
    }

    public void getUserInfoData(){
        personalDataServices.getTeacherInfo(loadingInfo,teacherInfoContainer,teacherInfoBtn,
                dataStore.getTeacher_id(),collegeLabel,teacherNumber,teacherName,teacherPhone,
                teacherMail,teacherPassword,college);
    }

    public void modifyTeacherInfo() {
        if((teacherNumber.getText()!=null&&!teacherNumber.getText().trim().equals(""))
                &&(teacherName.getText()!=null&&!teacherName.getText().trim().equals(""))
                &&(teacherPhone.getText()!=null&&!teacherPhone.getText().trim().equals(""))
                &&(teacherMail.getText()!=null&&!teacherMail.getText().trim().equals(""))
                &&(teacherPassword.getText()!=null&&!teacherPassword.getText().trim().equals(""))
                &&selectCollegeId!=null){
            personalDataServices.modifyTeacherInfo(teacherNumber.getText(),teacherName.getText(),
                    teacherPhone.getText(),teacherMail.getText(),teacherPassword.getText(),
                    selectCollegeId,teacherInfoBtn);
        }else{
            new DialogBuilder(teacherInfoBtn).setTitle("提示").setMessage("关键信息不能为空！").
                    setPositiveBtn("确定","#ff3333").create();
        }

    }

    public void exitCourseGroup() {
        if(selectCourseGroupState.equals("已加入")||selectCourseGroupState.equals("待审核")){
            personalDataServices.exitCourseGroup(exitCourseGroupBtn,selectCourseGroupId);
        }else{
            new DialogBuilder(courseGroup).setTitle("提示").setMessage("错误的操作对象").
                    setPositiveBtn("确定","#ff3333").create();
        }
    }

    public void joinCourseGroup() {
        if(selectCourseGroupState.equals("未加入")){
            personalDataServices.joinCourseGroup(joinCourseGroupBtn,selectCourseGroupId);
        }else{
            new DialogBuilder(courseGroup).setTitle("提示").setMessage("错误的操作对象").
                    setPositiveBtn("确定","#ff3333").create();
        }
    }
}
